import { Color, Node, Sprite, tween, UIOpacity, v3, Vec3 } from "cc";

export namespace CommTweens {

    export function fadeOut(node: Node, duration = 0.3, delay = 0) {
        const op = node.getComponent(UIOpacity) || node.addComponent(UIOpacity);
        op.opacity = 255;
        return tween(op)
        .delay(delay)
        .to(duration, {
            opacity: 0,
        }, {
            easing: 'fade',
        });
    }
    export function fadeIn(node: Node, duration = 0.3, delay = 0) {
        const op = node.getComponent(UIOpacity) || node.addComponent(UIOpacity);
        op.opacity = 0;
        return tween(op)
        .delay(delay)
        .to(duration, {
            opacity: 255,
        }, {
            easing: 'fade',
        });
    }
    export function fadeTo(node: Node, opacity:number, duration = 0.3) {
        const op = node.getComponent(UIOpacity) || node.addComponent(UIOpacity);
        return tween(op)
        .to(duration, {
            opacity,
        }, {
            easing: 'fade',
        });
    }
    export function pop(node: Node, duration = 0.3,from = 0, to = 1) {
        node.setScale(from, from, 1);
        return tween(node)
            .to(duration, {
                scale: v3(to, to, 1)
            }, { easing: 'backOut' });
    }
    export function popNoEase(node: Node, duration = 0.3,from = 0, to = 1) {
        node.setScale(from, from, 1);
        return tween(node)
            .to(duration, {
                scale: v3(to, to, 1)
            });
    }
    export function zoomTo(node: Node, duration = 0.3,to = 1) {
        return tween(node)
            .to(duration, {
                scale: v3(to, to, 1)
            }, { easing: 'quadIn' });
    }
    export function popFadeIn(node: Node, duration = 0.3) {
        fadeIn(node, duration).start();
        pop(node, duration).start();
    }
    export function uiOpenScale(content: Node, mask?: Node, timeScale = 1) {
        return tween(content)
        .call(()=>{
            let op = content.getComponent(UIOpacity) || content.addComponent(UIOpacity);
            tween(op).set({opacity: 0})
                .to(0.2 * timeScale, { opacity: 255 }).start();
            if (mask) {
                let op = mask.getComponent(UIOpacity) || mask.addComponent(UIOpacity);
                tween(op).set({opacity: 0})
                    .to(0.2 * timeScale, { opacity: 255 }).start();
            }
        })
        .set({ scale: new Vec3(0.8, 0.8, 1)})
        .to(0.2 * timeScale, { scale: new Vec3(1.05 , 1.05, 1) }, { easing: "backOut" })
        .to(0.1 * timeScale, { scale: new Vec3(1 , 1, 1) });
    }

    export function uiCloseScale(content: Node, mask?: Node) {
        return tween(content)
            .call(()=>{
                let op = content.getComponent(UIOpacity) || content.addComponent(UIOpacity);
                tween(op).set({opacity: 255})
                    .to(0.2, { opacity: 0 }).start();
                if (mask) {
                    let op = mask.getComponent(UIOpacity) || mask.addComponent(UIOpacity);
                    tween(op).set({opacity: 255})
                        .to(0.2, { opacity: 0 }).start();
                }
            })
            .set({ scale: new Vec3(1 , 1, 1)})
            .to(0.1, { scale: new Vec3(1.05 , 1.05, 1) }, { easing: "backOut" })
            .to(0.2, { scale: new Vec3(0.8 , 0.8, 1) });
    }
    /**向上飘动并淡出 */
    export function moveUpFade(node: Node, duration = 0.5, y = 100) {
        return tween(node).call(()=>{
            fadeOut(node, duration - 0.2, 0.2).start();
        }).by(duration, { position: v3(0, y, 0) }
        , { easing: 'cubicOut' });
    }

    /**旋转动画 */
    export function rotateCard(node: Node, duration = 0.5, isLeft: boolean = true, angle: number) {
        return tween(node).call(()=>{
            const targetAngle = node.angle + (isLeft ? -angle : angle);
            tween(node).to(duration, { angle: targetAngle }, { easing: 'quadOut' }).start();
        })
    }
}